//
//  HACNodesComponentSimpleIndex.m
//  HStockCharts
//
//  Created by Hotacool on 2018/10/30.
//

#import "HACNodesComponentSimpleIndex.h"
#import "HACMLineNode.h"
#import "HACTecNode.h"

#define KNode (self.nodeDic[kMainNodeKey])
static const CGFloat kTimeBarHeight = 16;
static const int kLineCount = 241;
static NSString *const kMainNodeKey = @"kMainNodeKey";
static NSString *const kSubNodeKey = @"kSubNodeKey";

@interface HACNodesComponentSimpleIndex ()
/** 基本数据 */
@property (nonatomic, strong) HACBasicInfoEntry *basicInfoData;
@end
@implementation HACNodesComponentSimpleIndex
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initialNodes];
        [self updateFrame:frame];
    }
    return self;
}

- (void)initialNodes {
    self.contentScroll.scrollEnabled = NO;
    self.visualCount = kLineCount;
    HACMLineNode *mainNode = [[HACMLineNode alloc] initWithHostLayer:self.layer];
    mainNode.visualCount = kLineCount;
    mainNode.xAxisCount = 3;
    [self.nodeDic setObject:mainNode forKey:kMainNodeKey];
    HACTecNode *tecNode = [[HACTecNode alloc] initWithHostLayer:self.layer tecType:HACTecTypeVolume];
    tecNode.itemsSpace = 2;
    [self.nodeDic setObject:tecNode forKey:kSubNodeKey];
    if (mainNode) {
        tecNode.visualCount = mainNode.visualCount;
        tecNode.relativeFrame = mainNode.relativeFrame;
        tecNode.pointRect = mainNode.pointRect;
        tecNode.startIndex = mainNode.startIndex;
        tecNode.endIndex = mainNode.endIndex;
    } else {
        tecNode.visualCount = self.visualCount;
    }
}

- (void)setData:(HACChartData *)data {
    HACChartDataSet *tmp = [data dataSetWithLabel:HACBasicDataLabel];
    self.basicInfoData = tmp?(HACBasicInfoEntry*)tmp.values.firstObject:nil;
    [super setData:data];
    [self updateFrame:self.frame];
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    [self updateFrame:frame];
}

- (void)updateFrame:(CGRect)frame {
    self.marketTimeBar.frame = CGRectMake(0, frame.size.height - kTimeBarHeight, frame.size.width, kTimeBarHeight);
    frame = CGRectMake(0, 0, frame.size.width, frame.size.height - kTimeBarHeight);
    //重置frame、relativeFrame、pointrect
    CGFloat mainNodeRate = 0.6;
    HACChartNode *node = KNode;
    node.relativeFrame = CGRectMake(0, 0, frame.size.width, frame.size.height*mainNodeRate);
    [node setNeedsDisplay];
    CGFloat originY = CGRectGetMaxY(node.relativeFrame);
    CGFloat tecNodeHeight = frame.size.height - KNode.relativeFrame.size.height;
    node = self.nodeDic[kSubNodeKey];
    node.relativeFrame = CGRectMake(0, originY, frame.size.width, tecNodeHeight);
    [node setNeedsDisplay];
}

- (void)reactForLongPressGesture:(UILongPressGestureRecognizer *)gesture {
    CGPoint point = [gesture locationInView:self];
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        long tmpIndex = [KNode indexAtChartForCoordinate:point];// node内部index
        self.focusPoint = [KNode pointAtChartForIndex:tmpIndex];
        self.focusIndex = tmpIndex + KNode.startIndex;
    } else {
        self.focusIndex = -1;
    }
}

- (NSArray<NSString *> *)stringForFocusItem:(HACFocusComponents *)focusItem focusIndex:(NSInteger)focusIndex {
    NSArray *retArray;
    NSString *focusString, *focusExtString;
    HACTimeLineEntry *data = [KNode dataAtChartForIndex:focusIndex];
    if (focusItem.axisType == DrawingAxisTypeX) {
        focusString = @(data.newprice).stringValue;
        double midValue = self.basicInfoData.dpreviousPrice;
        double yValue = data.newprice;
        yValue = (midValue>0)?(yValue - midValue)/midValue*100:0;
        focusExtString = [NSString stringWithFormat:@"%.2f%%", yValue];
    } else {
        focusString = @(data.date).stringValue;
        if (focusString.length == 12) {
            focusString = [NSString stringWithFormat:@"%@:%@", [focusString substringWithRange:NSMakeRange(8, 2)], [focusString substringWithRange:NSMakeRange(10, 2)]];
        }
        focusExtString = focusString;
    }
    if (focusString && focusExtString) {
        retArray = @[focusString, focusExtString];
    }
    return retArray;
}
@end
